import {ref} from "vue";
import axios from "@/utils/axios";
import {message} from "ant-design-vue";

class TampDeviceInfo{
    deviceId: number|undefined = -1
    weightPointFirst: number|undefined
    diameterPointFirst: number|undefined
    weightPointSecond: number|undefined
    diameterPointSecond: number|undefined
    weightCover: number|undefined
    diameterCover: number|undefined
}

export default ()=>{
    const tampFormRef = ref()
    const showTampInfoModal = ref(false)
    const tampFormState = ref(new TampDeviceInfo())

    const numberRule = (rule:any,value:any,callback:any)=>{
        if (Number.isFinite(value) && Number(value)<0) {
            callback('请输入大于等于0的数字')
        }else {
            callback()
        }
    }

    const tampFormRules = {
        // weightPointFirst:[{
        //     message:'请输入大于0的数字',
        //     trigger:'blur'
        // }],
        // diameterPointFirst:[{
        //     validator: numberRule,
        //     message:'请输入大于0的数字',
        //     trigger:'blur'
        // }],
        // weightPointSecond:[{
        //     validator: numberRule,
        //     message:'请输入大于0的数字',
        //     trigger:'blur'
        // }],
        // diameterPointSecond:[{
        //     validator: numberRule,
        //     message:'请输入大于0的数字',
        //     trigger:'blur'
        // }],
        // weightCover:[{
        //     validator: numberRule,
        //     message:'请输入大于0的数字',
        //     trigger:'blur'
        // }],
        // diameterCover:[{
        //     validator: numberRule,
        //     message:'请输入大于0的数字',
        //     trigger:'blur'
        // }]
    }


    /**
     * 获取强夯设备详细信息
     * @param deviceId 设备id
     * @param callback 回调函数
     */
    const getTampInfo = (deviceId:number, callback: Function|null = null)=>{
        tampFormState.value.deviceId = deviceId
        axios.post("/tamp/findDeviceParameterByDeviceId",{deviceId: deviceId})
            .then((res:any)=>{
                if(res.success) Object.assign(tampFormState.value, res.data)
                if(callback) callback()
            })
    }

    /**
     * 设置强夯设备详细信息
     */
    const setTampInfo = (callback:Function|null=null)=>{
        tampFormRef.value.validate().then((res1:any)=>{
            axios.post("/tamp/saveDeviceParameterByDeviceId",tampFormState.value)
                .then((res:any)=>{
                    if(res.success){
                        message.info("保存成功")
                        if(callback) callback()
                    }else{
                        message.info(res.msg)
                    }
                })
        }).catch((err:any)=>{
            console.log(err)
        })
    }

    /**
     * 打开强夯详情设置表单
     */
    const openTampSetModal = (deviceId:number)=>{
        tampFormRef.value.resetFields()
        tampFormState.value = new TampDeviceInfo()
        getTampInfo(deviceId, ()=>{
            showTampInfoModal.value = true
        })
    }

    return {
        tampFormRef,
        showTampInfoModal,
        tampFormState,
        tampFormRules,

        setTampInfo,
        openTampSetModal
    }
}